from base.web import SHandler, get_files, BaseHandler, UserHandler

class create_user(SHandler):
    def post(self):
        c_list = ['name', 'address', 'phone', 'shortname']
        params = []
        for key in c_list:
            value = self.get_argument(key, '')
            params.append('\'' + value + '\'')
        condition = [
            ','.join(c_list),
            ','.join(params)
        ]
        if self.insert_sql(['insert into hs_user({0}) values ({1})'.format(*condition)]):
            res = self.res_success('insert')
        else:
            res = self.res_error('insert')
        self.write(res)

class query_user(SHandler):
    def set_params(self):
        name = self.get_argument('name', '')
        type = int(self.get_argument('type', 0))
        current_page = int(self.get_argument('current_page', '1'))
        page_size = int(self.get_argument('page_size', 100000))
        if type == 0:
            q = ''
        elif type == 1:
            q = 'and openid != \'0\''
        elif type == 2:
            q = 'and openid = \'0\''
        self.condition = dict(
            name = name,
            start = (current_page - 1) * page_size,
            num = page_size,
            q = q
        )
        print(self.condition)
    def get_users(self):
        self.data = self.query_sql('''select * from hs_user where (name like \'%{name}%\' or 
            shortname like \'%{name}%\' or phone like \'%{name}%\') {q} limit {start}, {num}
            '''.format(**self.condition))
    def get_total(self):
        self.total = self.query_sql('''select count(*) as total from hs_user where (name like \'%{name}%\' or 
            shortname like \'%{name}%\' or phone like \'%{name}%\') {q}
            '''.format(**self.condition))[0]['total']
    def post(self):
        try:
            self.set_params()
            self.get_users()
            self.get_total()
            res = self.res_success('query', data = self.data, object=dict(total = self.total))
        except:
            res = self.res_error('query')
        self.write(res)

class modify_user(SHandler):
    def post(self):
        id = int(self.get_argument('id', 0))
        # c_list = ['name', 'address', 'phone', 'shortname']
        c_list = ['phone', 'address']
        params = []
        for key in c_list:
            value = self.get_argument(key, '')
            if value:
                params.append(key + '=\'' + value + '\'')
        condition = [
            ','.join(params),
            id
        ]
        if self.modify_sql(['update hs_user set {0} where id = {1}'.format(*condition)]):
            res = self.res_success('modify')
        else:
            res = self.res_error('modify')
        self.write(res)

class user_detail(UserHandler):
    def post(self):
        id = self.userid or self.get_argument('id', 0)
        user = self.query_sql('select * from hs_user where id = {0}'.format(id))[0]
        if user:
            res = self.res_success('query', object = dict(user = user))
        else:
            res = self.res_error('query')
        self.write(res)

user_url = [
    (r'/api/user/create_user', create_user),
    (r'/api/user/query_user', query_user),
    (r'/api/user/modify_user', modify_user),
    (r'/api/user/user_detail', user_detail),
]